package com.maodou.core.navigation

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList

interface NavKey

/**
 * 导航动画类型
 */
enum class NavigationAnimation {
    SLIDE,  // 默认的左右滑动
    FADE,   // 淡入淡出
    SCALE,  // 缩放
    NONE    // 无动画
}

class BackStack<T : Any>(initial: T? = null) {
    private val _stack: SnapshotStateList<T> = mutableStateListOf<T>().apply {
        initial?.let { add(it) }
    }

    val stack: SnapshotStateList<T> get() = _stack

    fun navigate(key: T) { _stack.add(key) }

    fun pop(): Boolean = if (_stack.isNotEmpty()) {
        _stack.removeAt(_stack.lastIndex)
        true
    } else {
        false
    }

    fun replaceTop(key: T) {
        if (_stack.isEmpty()) {
            _stack.add(key)
        } else {
            _stack[_stack.lastIndex] = key
        }
    }

    fun resetTo(key: T) {
        _stack.clear()
        _stack.add(key)
    }

    val canGoBack: Boolean get() = _stack.size > 1
}

@Composable
fun <T : Any> rememberBackStack(start: T): BackStack<T> = remember { BackStack(start) }


object Navigator3 {
    @Volatile
    private var backStack: SnapshotStateList<Any>? = null
    
    @Volatile
    private var _nextNavigationAnimation: NavigationAnimation? = null
    @Volatile
    private var _nextPopAnimation: NavigationAnimation? = null

    fun bind(stack: SnapshotStateList<Any>) {
        backStack = stack
    }

    fun unbind() {
        backStack = null
    }

    fun navigate(key: Any, animation: NavigationAnimation = NavigationAnimation.SLIDE) {
        _nextNavigationAnimation = animation
        backStack?.add(key)
    }

    fun pop(animation: NavigationAnimation = NavigationAnimation.SLIDE) {
        val s = backStack ?: return
        if (s.size > 1) {
            _nextPopAnimation = animation
            s.removeAt(s.lastIndex)
        }
    }

    fun replaceTop(key: Any, animation: NavigationAnimation = NavigationAnimation.SLIDE) {
        val s = backStack ?: return
        _nextNavigationAnimation = animation
        if (s.isEmpty()) s.add(key) else s[s.lastIndex] = key
    }

    fun resetTo(key: Any, animation: NavigationAnimation = NavigationAnimation.SLIDE) {
        val s = backStack ?: return
        _nextNavigationAnimation = animation
        s.clear()
        s.add(key)
    }

    /**
     * 使用淡入淡出动画重置到指定页面
     * 适用于启动页等需要特殊动画效果的场景
     */
    fun resetToWithFade(key: Any) {
        resetTo(key, NavigationAnimation.FADE)
    }

    /**
     * 获取并消费下一次导航动画类型
     */
    internal fun consumeNavigationAnimation(): NavigationAnimation {
        val animation = _nextNavigationAnimation ?: NavigationAnimation.SLIDE
        _nextNavigationAnimation = null
        return animation
    }

    /**
     * 获取并消费下一次返回动画类型
     */
    internal fun consumePopAnimation(): NavigationAnimation {
        val animation = _nextPopAnimation ?: NavigationAnimation.SLIDE
        _nextPopAnimation = null
        return animation
    }

    fun canGoBack(): Boolean = (backStack?.size ?: 0) > 1
}